package com.droidblackbox.history;

import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.droidblackbox.info.BlackBox;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MyHistoryOverlay extends Overlay {

	private Activity activity = null;
	private List<Object> list = null;

	private Point moveToPoint = null;
	private Point lineToPoint = null;
	private Paint paint = null;
	private Path path = null;

	private BlackBox moveBlackBox = null;
	private BlackBox lineBlackBox = null;

	public MyHistoryOverlay(Activity activity, List<Object> list) {
		this.activity = activity;
		this.list = list;
		path = new Path();

		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setDither(true);
		paint.setColor(Color.RED);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStrokeWidth(10);
		paint.setTextSize(330);
		paint.setStyle(Paint.Style.FILL);

	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);

		Projection projection = mapView.getProjection();

		Paint bitmapPaint = new Paint();
		bitmapPaint.setAntiAlias(true);

		Bitmap startBitmap = BitmapFactory
				.decodeResource(activity.getResources(),
						com.droidblackbox.R.drawable.map_start);

		BlackBox startBlackBox = getBlackBox(list.get(0));

		Point startPoint = projection.toPixels(
				new GeoPoint(startBlackBox.getLatitude(), startBlackBox
						.getLongitude()), null);

		startPoint.x = startPoint.x - (startBitmap.getWidth() / 5);
		startPoint.y = startPoint.y - (startBitmap.getHeight() / 1);

		canvas.drawBitmap(startBitmap, startPoint.x, startPoint.y, bitmapPaint);

		Bitmap endBitmap = BitmapFactory.decodeResource(
				activity.getResources(), com.droidblackbox.R.drawable.map_end);
		BlackBox endBlackBox = getBlackBox(list.get(list.size() - 1));

		Point endPoint = projection.toPixels(
				new GeoPoint(endBlackBox.getLatitude(), endBlackBox
						.getLongitude()), null);

		endPoint.x = endPoint.x - (endBitmap.getWidth() / 5);
		endPoint.y = endPoint.y - (endBitmap.getHeight() / 1);

		for (int i = 0; i < list.size(); i++) {
			if (i + 1 == list.size()) {
				// Last
				moveBlackBox = getBlackBox(list.get(i));
				lineBlackBox = getBlackBox(list.get(i));
			} else {
				moveBlackBox = getBlackBox(list.get(i));
				lineBlackBox = getBlackBox(list.get(i + 1));
			}

			int moveLatitude = moveBlackBox.getLatitude();
			int moveLongitude = moveBlackBox.getLongitude();

			int lineLatitude = lineBlackBox.getLatitude();
			int lineLongitude = lineBlackBox.getLongitude();

			moveToPoint = projection.toPixels(new GeoPoint(moveLatitude,
					moveLongitude), null);
			lineToPoint = projection.toPixels(new GeoPoint(lineLatitude,
					lineLongitude), null);

			path.reset();
			canvas.drawPath(path, paint);

			Path p = new Path();
			p.reset();
			p.moveTo(moveToPoint.x, moveToPoint.y);
			p.lineTo(lineToPoint.x, lineToPoint.y);

			path.addPath(p);
			paint.setStyle(Paint.Style.STROKE);
			canvas.drawPath(path, paint);

			if (lineBlackBox.isAccident() == true) {
				Bitmap bitmap = BitmapFactory.decodeResource(
						activity.getResources(),
						com.droidblackbox.R.drawable.map_accident);

				BlackBox blackBox = lineBlackBox;

				Point point = projection.toPixels(
						new GeoPoint(blackBox.getLatitude(), blackBox
								.getLongitude()), null);

				point.x = point.x - (bitmap.getWidth() / 5);
				point.y = point.y - (bitmap.getHeight() / 1);

				if (point.x != endPoint.x || point.y != endPoint.y)
					canvas.drawBitmap(endBitmap, endPoint.x, endPoint.y,
							bitmapPaint);

				canvas.drawBitmap(bitmap, point.x, point.y, bitmapPaint);

			} else {
				canvas.drawBitmap(endBitmap, endPoint.x, endPoint.y,
						bitmapPaint);
			}

		}
	}

	private BlackBox getBlackBox(Object object) throws ClassCastException {
		return (BlackBox) object;
	}

}
